<?php
    /**
     * Created by PhpStorm.
     * User: huangzhongwei
     * Date: 2018/11/5
     * Time: 19:19
     */

    namespace app\api\controller;

    use app\api\service\Token;
    use app\api\validate\IDMustBePositiveInt;
    use app\api\validate\PagingParameter;
    use app\common\model\Account;
    use app\api\validate\Withdrawal as WithdrawalValidate;
    use app\lib\exception\MissException;
    use app\lib\exception\ParameterException;
    use app\common\model\Withdrawal as WithdrawalModel;
    use app\lib\exception\SuccessMessage;
    use app\lib\exception\WithdrawalException;


    class Withdrawal extends BaseController
    {

        /**
         * 金额提现
         * @return SuccessMessage
         * @throws ParameterException
         * @throws \think\Exception\DbException
         */
        public function create()
        {
            (new WithdrawalValidate())->goCheck();
            $uid     = Token::getCurrentUid();
            $account = Account::get($uid);
            $money   = input('post.money');
            if ($money > $account->money) {
                throw new ParameterException(['msg' => '提现余额不足', 'errorCode' => 70000]);
            }
            if ($account->money < 100) {
                throw new ParameterException(['msg' => '提现余额不能小于100', 'errorCode' => 70001]);
            }

            $withdrawalModel = new WithdrawalModel();

            $withdrawalModel->account_id = $uid;

            $withdrawalModel->money = $money;

            $withdrawalModel->save();



//            $account->money -= $money;
//
//            $account->save();


            return new SuccessMessage();
        }


        /**
         * 获取提现记录
         * @param $page
         * @param $size
         *
         * @return array
         * @throws ParameterException
         */
        public function getRecordList($page, $size)
        {
            $uid        = Token::getCurrentUid();
            $recordList = WithdrawalModel::getRecordList($uid, $page, $size);
            if ($recordList->isEmpty()) {
                return [
                    'current_page' => $recordList->currentPage(),
                    'data'         => [],
                ];
            }
            $data = $recordList->toArray();
            return [
                'current_page' => $recordList->currentPage(),
                'data'         => $data,
            ];
        }


        /**
         *
         * 获取所有提现记录
         * @param $page
         * @param $size
         *
         * @return array
         * @throws \think\exception\DbException
         */
        public function getAllRecordList($page,$size){
            (new PagingParameter())->goCheck();
            $users = (new  WithdrawalModel())->with('account')->order('create_time', 'desc')->paginate(
                $size, false, [
                'page' => $page,
            ]);
            if ($users->isEmpty()) {
                // 对于分页最好不要抛出MissException，客户端并不好处理
                return [
                    'current_page' => $users->currentPage(),
                    'data'         => [],
                ];
            }
            $data = $users->toArray();
            return [
                'current_page' => $users->currentPage(),
                'data'         => $data,
            ];
        }

        /**
         * 审核通过
         * @return SuccessMessage
         * @throws MissException
         * @throws ParameterException
         * @throws \think\Exception\DbException
         */

        public function statePass(){
            (new IDMustBePositiveInt())->goCheck();
            $id=input('post.id');
            $withdrawal=(new WithdrawalModel())->get($id);
            if(!$withdrawal){
                throw new MissException(['msg'=>'当前提现记录不存在','errorCode'=>100000]);
            }

            //线下审核成功 扣除跑男金额
            if($withdrawal->state!=2){
                $account=(new Account())->get($withdrawal->account_id);

                $account->money -= $withdrawal->money;
                if($account->money<0){
                    throw new WithdrawalException();
                }

                $account->save();
            }

            $withdrawal->state=2;
            $withdrawal->save();

            return new SuccessMessage();
        }

        /**
         * 审核拒绝
         * @return SuccessMessage
         * @throws MissException
         * @throws ParameterException
         * @throws \think\Exception\DbException
         */
        public function stateRefuse(){
            (new IDMustBePositiveInt())->goCheck();
            $id=input('post.id');
            $withdrawal=(new WithdrawalModel())->get($id);
            if(!$withdrawal){
                throw new MissException(['msg'=>'当前提现记录不存在','errorCode'=>100000]);
            }

            //提现从审核通过 到拒绝 提现金额 返回到用户账户
            if($withdrawal->state==2){
                $account=(new Account())->get($withdrawal->account_id);

                $account->money += $withdrawal->money;

                $account->save();
            }

            $withdrawal->state=1;
            $withdrawal->save();
            return new SuccessMessage();
        }

    }